/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helpdesk.beans;

import com.helpdesk.entities.Accounts;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author JK
 */
@Stateless
public class AccountsFacade extends AbstractFacade<Accounts> {

    @PersistenceContext(unitName = "HelpDeskPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public AccountsFacade() {
        super(Accounts.class);
    }

    public boolean validateUser(String username, String password) {
        Query query = em.createNamedQuery("Accounts.findByUsername");
        query.setParameter("username", username);
        Accounts acc = (Accounts) query.getSingleResult();
        if (acc == null) {
            return false;
        }
        return acc.getPassword().equals(password);
    }

    public Accounts findByEmail(String email) {
        Query query = em.createNamedQuery("Accounts.findByEmail");
        query.setParameter("email", email);
        return (Accounts) query.getSingleResult();
    }

    public Accounts findByUsername(String username) {
        Query query = em.createNamedQuery("Accounts.findByUsername");
        query.setParameter("username", username);
        List<Accounts> result = query.getResultList();
        if (result.size() > 0) {
            return result.get(0);

        }
        return null;
    }
    public List<Accounts> findByRole(int role){
        Query query = em.createNamedQuery("Accounts.findByRoles");
        query.setParameter("roles", role);
        return query.getResultList();
    }
}
